/**
 * 菜单快捷访问自定义字段
 * 
 * @author PSI
 * @copyright 2015 - present
 * @license GPL v3
 */
PCL.define("PSI.Home.MainMenu.ShortcutField", {
  extend: "PCL.form.field.ComboBox",
  alias: "widget.psi_mainmenushortcutfield",

  mixins: ["PSI.AFX.Mix.Common"],

  initComponent() {
    const me = this;

    const modelName = me.buildModelName(me, "MenuShortcutField");
    PCL.define(modelName, {
      extend: "PCL.data.Model",
      fields: ["id", "fid", "caption", "py", "code"]
    });

    const store = PCL.create("PCL.data.Store", {
      autoLoad: false,
      model: modelName,
      data: [],
      pageSize: 20,
      remoteFilter: true,
      proxy: {
        type: "ajax",
        actionMethods: {
          read: "POST"
        },
        url: me.URL("Home/MainMenu/queryDataForShortcut2"),
        reader: {
          root: 'dataList',
          totalProperty: 'totalCount'
        }
      },
      listeners: {
        beforeload: {
          fn() {
            store.proxy.extraParams = {
              queryKey: me.getValue(),
            };
          },
          scope: me
        }
      }
    });

    PCL.apply(me, {
      store,
      displayField: 'py',
      typeAhead: false,
      hideLabel: true,
      hideTrigger: true,
      anchor: '100%',
      minChars: 0,
      listConfig: {
        matchFieldWidth: false,
        loadingText: '请稍等...',
        emptyText: `<h3 style='color:red;font-size:16px'>没有查询到记录</h3>`,
        minWidth: 300,
        getInnerTpl() {
          return `
           <strong style='color:#427cac;font-size:16px'>{caption}</strong>
           <div>
              <span style='margin:10px;color:#8c8c8c;font-size:11px'>助记码 {py}</span>
              <tpl if="code">
                <span style='margin:10px;color:#8c8c8c;font-size:11px'>编码 {code}</span>
              </tpl>
           </div>
           `;
        }
      },

      listeners: {
        select: {
          fn(combo, records, eOpts) {
            const fid = records[0].data.fid;
            let url = PSI.Const.BASE_URL + "Home/MainMenu/navigateTo/fid/" + fid + "/t/2";
            if (fid == "-9995") {
              url = PSI.Const.BASE_URL + "Home/Help/index" + "/t/fromShortcut";
            }

            window.open(url);

            me.setValue(null);
          },
          scope: me
        }

      }
    });

    me.callParent(arguments);

    me.on({
      render(p) {
        p.getEl().on("click", () => {
          me.onTriggerClick();
        });
      },
      single: true
    });
  },
});
